<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form ref="userFormRef" label-width="220px" :model="userInfo" :rules="rules">
          <!-- 姓名 部门 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="userInfo.username" size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 工号 入职时间 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="工号" prop="workNumber">
                <!-- 工号是系统生成的  禁用这个组件-->
                <el-input v-model="userInfo.workNumber" disabled size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机 聘用形式  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input
                  v-model="userInfo.mobile"
                  size="mini"
                  class="inputW"
                  :disabled="!!$route.params.id"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="departmentId">
                <!-- 放置及联部门组件 -->
                <!-- <el-cascader :options="options" :props="props" separator="-" class="inputW" /> -->
                <select-tree v-model="userInfo.departmentId" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select v-model="userInfo.formOfEmployment" size="mini" class="inputW">
                  <el-option label="正式" :value="1" />
                  <el-option label="非正式" :value="2" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  v-model="userInfo.timeOfEntry"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker
                  v-model="userInfo.correctionTime"
                  size="mini"
                  type="date"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像">
                <!-- 放置上传图片 -->
                <ImageUpload v-model="userInfo.staffPhoto" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12" style="margin-left:220px">
              <el-button size="mini" type="primary" @click="saveData">保存更新</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { addEmployeeApi, getEmployeeDetailApi, updateEmployeeApi } from '@/api/employee'
import SelectTree from './components/select-tree.vue'
import ImageUpload from './components/image-upload.vue'

export default {
  components: {
    SelectTree,
    ImageUpload
  },
  data() {
    return {
      userInfo: {
        username: '',
        mobile: '',
        formOfEmployment: null,
        workNumber: '',
        departmentId: null,
        timeOfEntry: '',
        correctionTime: '',
        staffPhoto: ''
      },
      rules: {
        username: [
          { required: true, message: '员工名字不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '员工手机号不能为空', trigger: 'blur' },
          //   { pattern: '^1[3-9]\d{9}$', message: '手机号格式不正确', trigger: 'blur' }
          {
            //   pattern 正则表达式
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
          //   {}
        ],
        formOfEmployment: [
          { required: true, message: '员工聘用形式不能为空', trigger: 'blur' }
        ],
        departmentId: [
          { required: true, message: '员工部门id不能为空', trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '员工入职日期不能为空', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '员工转正日期不能为空', trigger: 'blur' },
          { validator: (rule, value, callback) => {
            if (value && this.userInfo.timeOfEntry) {
              const entryTime = new Date(this.userInfo.timeOfEntry)
              const correctTime = new Date(value)
              if (correctTime < entryTime) {
                callback(new Error('转正时间不能小于入职时间'))
              } else {
                callback()
              }
            } else {
              callback()
            }
          }, trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.$route.params.id && this.getEmployeeDetail()
  },
  methods: {
    async saveData() {
      await this.$refs.userFormRef.validate()
      if (this.$route.params.id) {
        await updateEmployeeApi(this.userInfo)
        this.$message.success('更新信息成功')
      } else {
        await addEmployeeApi(this.userInfo)
        this.$message.success('新增员工成功')
      }

      this.$router.push('/employee')
    },
    async getEmployeeDetail() {
      const res = await getEmployeeDetailApi(this.$route.params.id)
      this.userInfo = res
    }

  }

}
</script>

  <style scoped lang="scss">
      .edit-form {
        background: #fff;
        padding: 20px;
        .inputW {
          width: 380px
        }
      }

  </style>
